<?php

namespace app\web\controller;

use app\common\cachemodel\ArticleComment;
use app\common\cachemodel\ZlGoddessState;
use app\common\lib\Util;
use \think\Controller;
use \app\common\lib\Jssdk;
use \think\Env;
use Exception;

/**
 * 女神动态视频分享H5页面
 * Class VideoShare
 * @package app\web\controller
 */
class VideoShare extends Controller
{
    private $LOG_PRE = '女神动态视频分享web错误：';

    private $oss = '';
    private $domain = '';
    private $share_img = '';

    protected function _initialize(): void
    {
        parent::_initialize();
        $this->oss = Env::get('host.photo');
//        $this->domain = Env::get('host.domain');
        $this->domain = $this->oss;
        $this->share_img = $this->domain . '/static/video_share/images/share_logo.png';
    }

    public function index()
    {
        $id = input('id/d', 0);

        try
        {
            $page = 1;

            $resData = [];
            if ($page === 1)
            {

                $goddessState = (new ZlGoddessState())->getInfoById($id, ['type', 'url', 'thumb', 'title', 'publish_time', 'author', 'avatar', 'content', 'introduction', 'comment_num', 'good_num', 'collect_num', 'duration', 'read_num', 'read_base']);
                if (!$goddessState)
                {
                    return show(config('code.error'), '动态不存在');
                }

                $goodNum = (int)(($goddessState['good_num'] ?? 0) + floor(((($goddessState['read_num'] ?? 0) + ($goddessState['read_base'] ?? 0)) / 30)));
                $resData['article_info'] = [
                    'id' => $id,
                    'type' => (int)($goddessState['type'] ?? 0),
                    'url' => $goddessState['url'] ?? '',
                    'thumb' => $goddessState['thumb'] ?? '',
                    'title' => $goddessState['title'] ?? '',
                    'time' => Util::timeToStr($goddessState['publish_time']) ?? '',
                    'author' => $goddessState['author'] ?? '',
                    'avatar' => $goddessState['avatar'] ?? '',
                    'content' => htmlspecialchars_decode($goddessState['content'] ?? ''),
                    'introduction' => $goddessState['introduction'] ?? '',
                    'comment_num' => (int)($goddessState['comment_num'] ?? 0),
                    'good_num' => $goodNum < 0 ? 0 : $goodNum,
                    'collect_num' => ($goddessState['collect_num'] ?? 0) < 0 ? 0 : (int)($goddessState['collect_num'] ?? 0),
                    'duration' => (int)($goddessState['duration'] ?? 0),
                ];
                $resData['theme_info'] = null;

            }
            // 获取用户评论列表信息
            $data = (new ArticleComment())->getPageCommentList('', 2, $id, 2, $page);
            $data = array_merge($resData, $data);

            //test
//            $data['data'][1]['reply_list'][0]['nickname1'] = '杨阳';
//            $data['data'][1]['reply_list'][0]['nickname2'] = '月色';
//            $data['data'][1]['reply_list'][0]['content'] = '加油';
//
//            $data['data'][1]['reply_list'][1]['nickname1'] = '月色月色月色月色月色月色';
//            $data['data'][1]['reply_list'][1]['nickname2'] = '杨阳月色';
//            $data['data'][1]['reply_list'][1]['content'] = '好的';
//
//            $data['data'][1]['reply_list'][2]['nickname1'] = '月色';
//            $data['data'][1]['reply_list'][2]['nickname2'] = '杨阳';
//            $data['data'][1]['reply_list'][2]['content'] = '好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的好的';

            $signPackage = (new Jssdk())->getSignPackage();
            return $this->fetch('index', [
                'title' => $resData['article_info']['title'],//网页标题
                'share_title' => $resData['article_info']['title'],//微信分享标题
                'share_introduction' => $resData['article_info']['introduction'],//微信分享内容
                'share_img' => $this->share_img,
                'signPackage' => $signPackage,//微信签名
                'data' => $data,//带过去的数据
                'oss' => $this->oss,
                'domain' => $this->domain
            ]);
        }
        catch (Exception $e)
        {
            throw new Exception('[' . $e->getFile() . ':' . $e->getLine() . ']' . PHP_EOL . $this->LOG_PRE . 'id=' . $id . $e->getMessage());
        }

    }

}
